package com.android.videotest.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.videotest.R;

public class MatrixTestActivity extends AppCompatActivity implements View.OnClickListener {
    public static void start(Context context) {
        Intent starter = new Intent(context, MatrixTestActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    private ImageView imageView;
    private Button btnReset, btnScale, btnRotate, btnTranslate, btnPostTrans, btnPreTrans;
    private Bitmap mBitmap, newBitmap;
    private Paint mPaint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matrix_test);
        initViews();
        btnReset.setOnClickListener(this);
        btnScale.setOnClickListener(this);
        btnRotate.setOnClickListener(this);
        btnTranslate.setOnClickListener(this);
        btnPostTrans.setOnClickListener(this);
        btnPreTrans.setOnClickListener(this);
        bitmapReset();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    private void initViews() {
        imageView = (ImageView) findViewById(R.id.imageView);
        btnReset = (Button) findViewById(R.id.btn_reset);
        btnScale = (Button) findViewById(R.id.btn_scale);
        btnRotate = (Button) findViewById(R.id.btn_rotate);
        btnTranslate = (Button) findViewById(R.id.btn_translate);
        btnPostTrans = (Button) findViewById(R.id.btn_post_trans);
        btnPreTrans = (Button) findViewById(R.id.btn_pre_trans);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reset:
                bitmapReset();
                break;
            case R.id.btn_scale:
                bitmapScale(0.5f, 0.5f);
                break;
            case R.id.btn_rotate:
                bitmapRotate(45);
                break;
            case R.id.btn_translate:
                bitmapTranslate(100, 100);
                break;
            case R.id.btn_post_trans:
                bitmapScaleAndTranslate(100, 100, 0.5f, 0.5f);
                break;
            case R.id.btn_pre_trans:
                bitmapTranslateAndRScale(100, 100, 0.5f, 0.5f);
                break;
            default:
                break;
        }
    }

    private void bitmapReset() {
        mBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.tiny),
                400, 400, false);
        imageView.setImageBitmap(mBitmap);
    }

    /**
     * 图片缩放
     */
    private void bitmapScale(float x, float y) {
        newBitmap = Bitmap.createBitmap((int) (mBitmap.getWidth() * x), (int) (mBitmap.getHeight() * y), mBitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Matrix matrix = new Matrix();
        matrix.setScale(x, y);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        imageView.setImageBitmap(newBitmap);
        mBitmap = newBitmap;
    }

    /**
     * 图片旋转
     */
    private void bitmapRotate(float degrees) {
        newBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Matrix matrix = new Matrix();
        matrix.setRotate(degrees, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        imageView.setImageBitmap(newBitmap);
        mBitmap = newBitmap;
    }

    /**
     * 图片平移
     */
    private void bitmapTranslate(float dx, float dy) {
        newBitmap = Bitmap.createBitmap((int) (mBitmap.getWidth() + Math.abs(dx)), (int) (mBitmap.getHeight() + Math.abs(dy)), mBitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Matrix matrix = new Matrix();
        matrix.setTranslate(dx, dy);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        imageView.setImageBitmap(newBitmap);
        mBitmap = newBitmap;
    }

    /**
     * 平移后缩放
     */
    private void bitmapTranslateAndRScale(float dx, float dy, float sx, float sy) {
        newBitmap = Bitmap.createBitmap((int) ((mBitmap.getWidth() + Math.abs(dx)) * sx), (int) ((mBitmap.getHeight() + Math.abs(dy)) * sy), mBitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Matrix matrix = new Matrix();
        matrix.setScale(sx, sy);
        matrix.preTranslate(dx, dy);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        imageView.setImageBitmap(newBitmap);
        mBitmap = newBitmap;
    }

    /**
     * 缩放后平移
     */
    private void bitmapScaleAndTranslate(float dx, float dy, float sx, float sy) {
        newBitmap = Bitmap.createBitmap((int) (mBitmap.getWidth() * sx + Math.abs(dx)), (int) (mBitmap.getHeight() * sy + Math.abs(dy)), mBitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Matrix matrix = new Matrix();
        matrix.setScale(sx, sy);
        matrix.postTranslate(dx, dy);
        canvas.drawBitmap(mBitmap, matrix, mPaint);
        imageView.setImageBitmap(newBitmap);
        mBitmap = newBitmap;
    }
}
